package day16;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class HomeWork {

}

class A3{
    private static A3 a3;
    private A3(){}
    public static synchronized A3 get(){
        if(a3 == null){a3 = new A3();}
        return a3;
    }
}

class TestA3{
    public static void main(String[] args) {
        for (int i = 0;i<10;i++){
            A3 a = A3.get();
            System.out.println(a);
        }
    }
}

//图形接口
interface Shapes{
    void draw();
}

//图形类
class Circle implements Shapes{

    @Override
    public void draw() {
        System.out.println("正在绘制圆形");
    }
}

class Rectangle implements Shapes{

    @Override
    public void draw() {
        System.out.println("正在绘制矩形");
    }
}
class Triangle implements Shapes{

    @Override
    public void draw() {
        System.out.println("正在绘制三角形");
    }
}

//图形工厂接口
interface GraphicsFactory{
    Shapes create();
}

//图形工厂类
class CircleFactory implements GraphicsFactory{

    @Override
    public Shapes create() {
        return new Circle();
    }
}

class RectangleFactory implements GraphicsFactory{

    @Override
    public Shapes create() {
        return new Rectangle();
    }
}
class TriangleFactory implements GraphicsFactory{

    @Override
    public Shapes create() {
        return new Triangle();
    }
}

//客户端
class graphicsUser{
    public static void main(String[] args) {
        one:
        while(true){
            System.out.println("请输入要创建的工厂:1.圆形2.矩形3.三角型4.取消");
            Scanner sc = new Scanner(System.in);
            int choice = sc.nextInt();
            GraphicsFactory factory;
            switch (choice) {
                case 1:factory = new CircleFactory();break;
                case 2:factory = new RectangleFactory();break;
                case 3:factory = new TriangleFactory();break;
                default:break one;
            }
            Shapes shapes = factory.create();
            shapes.draw();
        }
    }
}

abstract class People{
    String name;
    public abstract void update(String msg);
}

class Reader extends People{
    public Reader(String name){
        super.name=name;
    }

    public void update(String msg) {
        System.out.println(name+"获得通知:"+msg);
    }
}

interface New {
    void add(People user);
    void remove(People user);
    void update(String msg);
}

class NewsPublisher implements New {
    private List<People> list=new ArrayList<>();
    public void add(People user) {
        list.add(user);
        System.out.println(user.name+"添加关注");
    }
    public void remove(People user) {
        list.remove(user);
        System.out.println(user.name+"取消关注");
    }
    public void update(String msg) {
        System.out.println("新闻:"+msg);
        for(People user:list){
            user.update(msg);
        }
    }
    public static void main(String[] args) {
        NewsPublisher reader =new NewsPublisher();
        People user1=new Reader("读者1");
        People user2=new Reader("读者2");
        People user3=new Reader("读者3");
        reader.add(user1);
        reader.add(user2);
        reader.add(user3);
        reader.update("上课了");
        reader.remove(user2);
        reader.update("下课了");
    }
}

class UserMS {
    static Scanner sc = new Scanner(System.in);
    int ID;
    String username;
    String password;

    public UserMS(int ID, String username, String password) {
        this.ID=ID;
        this.username=username;
        this.password=password;
    }
    
    public static void main(String[] args) throws InterruptedException {
        List<UserMS> list=new ArrayList<>();
        //初始设置一个管理员账号
        UserMS user = new UserMS(0, "admin", "123456");
        list.add(user);
        Menu1(list);
    }

    public static void Menu1(List<UserMS> list) throws InterruptedException {
        System.out.println("***XXX用户管理系统***");
        System.out.println("1:用户注册 2:用户登录 其他:退出系统");
        int choice = sc.nextInt();
        switch (choice) {
            case 1:registered(list);break;
            case 2:login(list);break;
            default:System.out.println("系统已退出");break;
        }
    }

    public static void Menu2(List<UserMS> list,UserMS MyUser) throws InterruptedException {
        System.out.println("***XXX用户管理系统***");
        System.out.println("1:展示用户 2:修改密码 3:删除用户 其他:用户退出");
        int choice = sc.nextInt();
        switch (choice) {
            case 1:
                int n=0;
                Thread.sleep(1000);
                for (UserMS user : list) {
                    n++;
                    System.out.println("第"+n+"位用户ID:"+user.ID);
                    System.out.println("用户名:"+user.username);
                    System.out.println("密码:"+user.password);
                }
                System.out.println("查询成功！！！");
                Menu2(list,MyUser);
                break;
            case 2:
                System.out.print("请输入新密码:");
                MyUser.password = sc.next();
                Thread.sleep(1000);
                System.out.println("修改成功！！！");
                Menu2(list,MyUser);
                break;
            case 3:
                list.remove(MyUser);
                Thread.sleep(1000);
                System.out.println("删除成功！！！");
                Menu1(list);
                break;
            default:
                System.out.println("正在返回主菜单1");
                Thread.sleep(1000);
                Menu1(list);
                break;
        }
    }

    public static void registered(List<UserMS> list) throws InterruptedException {
        System.out.println("请输入账号和密码:");
        System.out.print("账号:");
        String name = sc.next();
        System.out.print("密码：");
        String password = sc.next();
        UserMS user = new UserMS(list.get(list.size()-1).ID+1, name, password);
        list.add(user);
        System.out.println("注册成功，即将返回主菜单1");
        Thread.sleep(1000);
        Menu1(list);
    }

    public static void login(List<UserMS> list) throws InterruptedException {
        int n = 0;
        while(n<3){
            System.out.println("请输入账号和密码:");
            System.out.print("账号:");
            String name = sc.next();
            System.out.print("密码：");
            String password = sc.next();
            for (UserMS user : list) {
                if(user.username.equals(name) && user.password.equals(password)){
                    System.out.println("登陆成功！！！");
                    System.out.println("登录成功，即将进入主菜单2");
                    Thread.sleep(1000);
                    Menu2(list,user);
                    return;
                }
            }
            n++;
            Thread.sleep(1000);
            System.out.println("登陆失败！！！");
            System.out.println("您还有"+(3-n)+"机会");
        }
        System.out.println("即将返回主菜单1");
        Thread.sleep(1000);
        Menu1(list);
    }
}


